{extend name='public/window' /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="form">
                <input type="hidden" name="__token__" id="token" value="{:token()}"/>
                {if isset($info['id'])}
                <input type="hidden" name="id" id="id" value="{$info['id']}"/>
                {/if}

                <div class="layui-form-item">
                    <label class="layui-form-label">标题 *</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入标题"
                               value="" class="layui-input">
                    </div>
                </div>
                {if empty($info['image'])}
                <div class="layui-form-item merch">
                    <label class="layui-form-label">图片 *</label>
                    <div style="width: 500px;height: 280px;display: inline-block; text-align: center;"
                         id="image1"><span
                            style="line-height: 280px;color: #ccc;font-size: 12px;">+点击添加图片</span>
                    </div>
                </div>
                {else/}
                <div class="layui-form-item merch">
                    <label class="layui-form-label">图片 *</label>
                    <div style="width: 500px;height: 280px;display: inline-block; text-align: center;"
                         id="image1"><img id="img1"  src="{$info.image}" style="width: 100%; height: 100%;" >
                    </div>
                </div>
                {/if}
                <div class="layui-form-item merch">
                    <label class="layui-form-label">内容 *</label>
                    <div class="layui-input-block">
                        {if !empty($info['content'])}
                        <textarea id="content" lay-verify="content"  name="content">{$info['content']}</textarea>
                        {else/}
                        <textarea id="content" lay-verify="content"  name="content"></textarea>
                        {/if}
                    </div>
                </div>
                <div class="layui-form-item merch">
                    <label class="layui-form-label">发送对象</label>
                    <div class="layui-input-inline">
                        <select name="send_who" lay-search="" id="send_who" lay-filter="send_who">
                            <option value="1">全体商户</option>
                            <option value="2">楼层区分</option>
                            <option value="3">业态区分</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item merch LC"  style="display: none">
                    <label class="layui-form-label">选择楼层</label>
                    <div class="layui-input-inline">
                        <select name="lcOrgid" lay-verify="required" lay-search=""  lay-filter="lcOrgid">
                            <option value="0" ></option>
                            {volist name="lcInfo" id="t"}
                            <option value="{$t.lcName}" >{$t.lcName}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                {if !empty($info['lcOrgid'])}
                <div class="layui-form-item merch LC QIELO">
                    <label class="layui-form-label">选择楼层</label>
                    <div class="layui-input-inline">
                        <select name="lcOrgid" lay-verify="required" lay-search=""  lay-filter="lcOrgid">
                            <option value="0" ></option>
                            {volist name="lcInfo" id="t"}
                            <option value="{$t.lcName}" >{$t.lcName}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                {/if}

                {if !empty($info['organization'])}
                <div class="layui-form-item yetai merch">
                    <label class="layui-form-label">业态</label>
                    <div class="layui-input-inline" >
                        <select id="organization" name="organization" lay-search="" lay-filter="organization">
                            <option value="0">请选择</option>
                            {volist name="orange" id="v"}
                            <option value="{$v.id}">{$v.name}</option>
                            {/volist}
                        </select>

                    </div>
                </div>
                {/if}
                <div class="layui-form-item yetai merch" style="display: none">
                    <label class="layui-form-label">业态</label>
                    <div class="layui-input-inline" >
                        <select id="organization" name="organization" lay-search="" lay-filter="organization">
                            <option value="0">请选择</option>
                            {volist name="orange" id="v"}
                            <option value="{$v.id}">{$v.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                {if !empty($info['merchant_list'])}
                <div class="layui-form-item" id="select_merchant_id">
                    <label class="layui-form-label">选择发送商户</label>
                    <div id="merchant_list" class="layui-input-block">
                        {volist name="merchant_list" id="v"}
                        <input type="checkbox" lay-skin="primary" value='{$v.id}' checked name="merchantlist" title='{$v.name}'>
                        {/volist}
                    </div>
                </div>

                {/if}
                {if empty($info['merchant_list'])}
                <div class="layui-form-item" id="select_merchant_id">
                    <label class="layui-form-label">选择发送商户</label>
                    <div id="merchant_list" class="layui-input-block">
                        所选条件所有商户
                    </div>
                </div>
                {/if}
                <div class="layui-form-item" id="select_merchant_id" style="display: none">
                    <label class="layui-form-label">选择发送商户</label>
                    <div id="merchant_list" class="layui-input-block">

                    </div>
                </div>
                <div class="layui-form-item" id="is_tax" >
                    <label class="layui-form-label">是否展示</label>
                    <div class="layui-input-block">
                        <input type="radio" name="is_show" value="1" title="是" lay-filter="is_show" checked>
                        <input type="radio" name="is_show" value="0" title="否" lay-filter="is_show">
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin " style="width: 100%">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0px;text-align: center;">
                            <button class="layui-btn" lay-submit="" lay-filter="add">{:__('Submit')}</button>
                            <button type="reset" class="layui-btn layui-btn-primary">{:__('Reset')}</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/html" id="operationTpl">
    <a href="javascript:;" class="layui-btn  layui-btn-danger layui-btn-xs" data-title="删除" lay-event="del"><i
            class="layui-icon "></i></a>
</script>
<script>
    layui.use(['element', 'form', 'table', 'jquery','upload', 'lucky', 'laydate', 'xmSelect', 'layedit'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        var upload = layui.upload;
        var lucky = layui.lucky;
        var layedit = layui.layedit;
        var id_lists = [];
        laydate.render({
            elem: '#send_time',
            type: 'datetime',
            trigger: 'click',
            min: minDate()
        });
        layedit.set({	//设置图片接口
            uploadImage: {
                url: "{:url('Common/UpEditorPic')}", //接口url
                type: 'post'
            }
        });
        //创建一个编辑器，lay_edit为textarea标签的id值
        var index = layedit.build('content', {
            width: 200,
            height: 350    //设置编辑器的高度
        });
        //赋值
        form.verify({
            content: function(value) {
                return layedit.sync(index);
            }
        });
        form.on('select(send_who)', function (data) {
            var type = data.value;
            console.log(type)
            if (type == 2) {
                $(".LC").css('display', 'block');
                $(".yetai").css('display', 'none');
                $("#select_merchant_id").css('display', 'block');
                $(".QIELO").css('display', 'none');
            } else if(type==3){
                $(".LC").css('display', 'none');
                $(".yetai").css('display', 'block');
                $("#select_merchant_id").css('display', 'block');
            } else {
                $(".LC").css('display', 'none');
                $(".yetai").css('display', 'none');
                $("#select_merchant_id").css('display', 'none');
            }
        });
        form.on('select(lcOrgid)', function (data) {
            $("#merchant_list").empty();
            form.render();
            var lcOrgid = data.value;
            console.log(lcOrgid)
            $.ajax({
                url:"{:url('getMerchantList')}",
                type:"post",
                data: {
                    "lcOrgid":lcOrgid,
                },
                dataType:"json",
                success:function(res){
                    console.log(res,'选了楼层')
                    $.each(res, function(index, item) {
                        //赋值
                        $('#merchant_list').append('<input type="checkbox" lay-skin="primary" value='+item['id']+' name="merchantlist" title='+item['name']+item['positions_code']+'>');
                    });
                    form.render();
                }
            });
        });
        form.on('select(organization)', function (data) {
            var value = $("#organization").val();
            $("#merchant_list").empty();
            $("#organization_one").empty();
            form.render();
            $.ajax({
                url:"{:url('change')}",
                method:'post',
                data:{
                    pid:value
                },
                success:function (res){
                    var info = res.data;
                    $.ajax({
                        url:"{:url('getMerchantList')}",
                        type:"post",
                        data: {
                            "organization":value,
                        },
                        dataType:"json",
                        success:function(res){
                            console.log(res,'选了一级业态')
                            $.each(res, function(index, item) {
                                console.log(item)
                                //赋值<input type="checkbox" name="like[write]" title="写作">
                                $('#merchant_list').append('<input type="checkbox" lay-skin="primary" value='+item['id']+' name="merchantlist" title='+item['name']+item['positions_code']+'>');
                            });
                            form.render();
                        }
                    });
                    if(info.length!=0){
                        info.forEach((elem, index) => {
                            $("#organization_one").append('<option value='+elem['id']+">"+elem['name']+'</option>');
                        });
                        //重新渲染
                        layui.form.render("select");
                        $('#show').show();

                    }else {
                        $('#show').hide()
                    }
                }
            })

        });

        form.on('select(organization_one)', function (data) {
            var value = $("#organization").val();
            var value1 = $("#organization_one").val();
            $("#merchant_list").empty();
            form.render();
            $.ajax({
                url:"{:url('getMerchantList')}",
                type:"post",
                data: {
                    "organization":value,
                    "organization_one":value1,
                },
                dataType:"json",
                success:function(res){
                    console.log(res,'选了二级业态')
                    $.each(res, function(index, item) {
                        //赋值
                        $('#merchant_list').append('<input type="checkbox" lay-skin="primary" value='+item['id']+' name="merchantlist" title='+item['name']+item['positions_code']+'>');
                    });
                    form.render();
                }
            });
        });
        //表单赋值
        var info = {:json_encode($info)};
        form.val("form",info);
        form.on('submit(add)', function (data) {
            var arr = new Array();
            $("input:checkbox[name='merchantlist']:checked").each(function(i){
                arr[i] = $(this).val();
            });
            //  替换 data.field.level的数据为拼接后的字符串
            data.field.merchant_list = arr.join(",");//将数组合并成字符串
            var image = $("#img1").attr("src");
            data.field.type = 2 // 添加type
            data.field.image = image // 添加type
            lucky.FormSubmit("{:url('add')}",data.field,true);
            return false;
        });
        /**
         *上传封面照片
         */
        upload.render({
            elem: '#image1',
            url: "{:url('common/UpNoticeImg')}" //图片上传地址
            ,
            accept: 'images' //
            ,
            acceptMime: 'image/*',
            size: 1024 * 12,
            before: function(res) {
                loading = layer.load(2, {
                    shade: [0.2, '#000'] //0.2透明度的白色背景
                });
            },
            done: function(data) {
                console.log(data)
                var image1 = document.getElementById('image1')
                image1.innerHTML = ` <img id="img1" src="" style="width: 100%; height: 100%;" >`
                layer.close(loading);
                if (data.code == 1) {
                    console.log(data)
                    $("#img1").attr("src", data.data.thumb_path);
                    layer.msg('图片上传成功', {
                        icon: 1,
                        time: 1000
                    });
                } else {
                    layer.msg(data.msg, {
                        icon: 5,
                        time: 1000
                    });
                }
            },
            error: function(red) {
                layer.close(loading);
                layer.msg("网络错误", {
                    icon: 5,
                    time: 1500
                });
            }
        });

    })
    function minDate() {
        var now = new Date();
        return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
    }

</script>

{/block}

